<%@page import="qldc.pm.tiensu.constants.ThaoTacUndoRedoEnum"%>
<%@page import="qldc.pm.tiensu.model.TEMP_TienSu"%>
<%@page import="qldc.pm.tiensu.service.TEMP_DoiTuongTienSuLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.service.TEMP_TienSuLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.model.TEMP_DoiTuongTienSu"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@page import="qldc.pm.qtht.dmdc.service.TonGiaoLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.model.TonGiao"%>
<%@page import="qldc.pm.qtht.dmdc.service.DanTocLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.model.DanToc"%>
<%@page import="qldc.pm.qtht.dmdc.enumclass.EnumGioiTinh"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="qldc.pm.csdl.cd90.service.CongDanDTOLocalServiceUtil"%>
<%@page import="qldc.pm.csdl.cd90.model.dto.CongDanDTO"%>
<%@page import="qldc.pm.qtht.dmdc.utils.DiaChiUtils"%>
<%@page import="qldc.pm.tiensu.service.DoiTuongTienSuLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.model.DoiTuongTienSu"%>
<%@page import="qldc.pm.tiensu.util.DateUtils"%>
<%@page import="qldc.pm.tiensu.service.TienSuLocalServiceUtil"%>
<%@include file="../../init.jsp"%>

<%
String redirect = ParamUtil.getString(request, "redirect");

String soDinhDanhCaNhan = ParamUtil.getString(request, "soDinhDanhCaNhan");

long doiTuongTienSuId = 0;//ParamUtil.getLong(request, "doiTuongTienSuId");

String hoVaTen = "";
String gioiTinhKey = "";
String soCMND = "";
String soSoHoKhau = "";
String noiThuongTru = "";
String ngaySinh = "";
String danToc = "";
String tonGiao = "";
String quocTich = "";
String hoVaTenCha = "";
String hoVaTenMe = "";

CongDanDTO congDanDTO = CongDanDTOLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCaNhan);

if(Validator.isNotNull(congDanDTO)) {
	hoVaTen = congDanDTO.getHoVaTen();
	
	gioiTinhKey = EnumGioiTinh.getKey(congDanDTO.getGioiTinh());
	
	soCMND = congDanDTO.getSoCMND();
	soSoHoKhau = congDanDTO.getSoSoHoKhau();
	
	long noiThuongTruQuanHuyenId = congDanDTO.getNoiThuongTruQuanHuyenId();
	long noiThuongTruPhuongXaId = congDanDTO.getNoiThuongTruPhuongXaId();
	String noiThuongTruDiaChi = congDanDTO.getNoiThuongTruDiaChi();
	
	noiThuongTru = DiaChiUtils.createBlockDiaChi(noiThuongTruQuanHuyenId, noiThuongTruPhuongXaId, noiThuongTruDiaChi);
	
	ngaySinh = congDanDTO.getNgaySinh();
	
	long danTocId = congDanDTO.getDanTocId();
	if(danTocId > 0) {
		DanToc dmdcDanToc = DanTocLocalServiceUtil.findById(danTocId);
		
		if(Validator.isNotNull(dmdcDanToc)) {
			danToc = dmdcDanToc.getTenDanToc();
		}
	}
	
	long tonGiaoId = congDanDTO.getTonGiaoId();
	if(tonGiaoId > 0) {
		TonGiao dmdcTonGiao = TonGiaoLocalServiceUtil.findById(tonGiaoId);
		
		if(Validator.isNotNull(dmdcTonGiao)) {
			tonGiao = dmdcTonGiao.getTenTonGiao();
		}
	}
	
	quocTich = congDanDTO.getQuocTich();
	hoVaTenCha = congDanDTO.getHoVaTenCha();
	hoVaTenMe = congDanDTO.getHoVaTenMe();
}
	DoiTuongTienSu doiTuongTienSu = DoiTuongTienSuLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCaNhan);
	if (Validator.isNotNull(doiTuongTienSu)){
		doiTuongTienSuId = doiTuongTienSu.getDoiTuongTienSuId();
	
}

PortletURL portletURL = renderResponse.createRenderURL();
portletURL.setParameter("mvcPath", "/html/view.jsp");
portletURL.setParameter("chonChucNangItem", TienSuConstants.QUAN_LY_NGUOI_CO_TIEN_SU);
%>

 
<liferay-ui:error key="thao-tac-thuc-hien-khong-thanh-cong" message="thao-tac-thuc-hien-khong-thanh-cong" />

<liferay-ui:success key="thao-tac-thuc-hien-thanh-cong" message="thao-tac-thuc-hien-thanh-cong" />

<portlet:renderURL var="currentUrl">
			<portlet:param name="mvcPath" value="/html/view.jsp" />
			<portlet:param name="chonChucNangItem" value="<%=TienSuConstants.QUAN_LY_NGUOI_CO_TIEN_SU %>" />
			<portlet:param name="tabs1" value="chi_tiet_tien_su" />
			<portlet:param name="doiTuongTienSuId" value="<%=String.valueOf(doiTuongTienSuId) %>" />
			<portlet:param name="soDinhDanhCaNhan" value="<%=soDinhDanhCaNhan %>" />
			<portlet:param name="redirect" value="<%=redirect.toString()%>" />
</portlet:renderURL>

<portlet:renderURL var="themTienSuURL">
	<portlet:param name="mvcPath" value="/html/view.jsp" />
	<portlet:param name="chonChucNangItem" value="<%=TienSuConstants.QUAN_LY_NGUOI_CO_TIEN_SU %>" />
	<portlet:param name="tabs1" value="cap_nhat_tien_su" />
	<portlet:param name="soDinhDanhCaNhan" value="<%=soDinhDanhCaNhan %>" />
	<portlet:param name="<%=Constants.CMD %>" value="<%=Constants.ADD %>" />
	<portlet:param name="redirect" value="<%=currentUrl.toString() %>" />
</portlet:renderURL>

<liferay-ui:header title="thong-tin-nguoi-co-tien-su" showBackURL="<%=false %>"></liferay-ui:header>

<table class="gridnormal">
	<tr>
		<td class="txtlabel aui-w20"><liferay-ui:message key="ho-va-ten" /></td>
		<td class="aui-w30"><font style="font-weight: bold;"><%=hoVaTen %></font></td>
		<td class="txtlabel aui-w20"><liferay-ui:message key="gioi-tinh" /></td>
		<td class="aui-w30"><font style="font-weight: bold;"><liferay-ui:message key="<%=gioiTinhKey %>"></liferay-ui:message></font></td>
	</tr>
	<tr>
		<td><liferay-ui:message key="so-so-ho-khau" /></td>
		<td><font style="font-weight: bold;"><%=soSoHoKhau%></font></td>
	</tr>
	<tr>
		<td><liferay-ui:message key="so-dinh-danh-ca-nhan" /></td>
		<td><font style="font-weight: bold;"><%=soDinhDanhCaNhan %></font></td>
		<td><liferay-ui:message key="so-cmnd" /></td>
		<td><font style="font-weight: bold;"><%=soCMND %></font></td>
	</tr>

	<tr>
		<td><liferay-ui:message key="ngay-sinh" /></td>
		<td><font style="font-weight: bold;"><%=ngaySinh %></font></td>
		<td><liferay-ui:message key="dan-toc" /></td>
		<td><font style="font-weight: bold;"><%=danToc %></font></td>
	</tr>
	
	<tr>
		<td><liferay-ui:message key="noi-thuong-tru" /></td>
		<td colspan="3"><font style="font-weight: bold;"><%=noiThuongTru %></font></td>		
	</tr>
	
	<tr>
		<td><liferay-ui:message key="ton-giao" /></td>
		<td><font style="font-weight: bold;"><%=tonGiao %></font></td>
		<td><liferay-ui:message key="quoc-tich" /></td>
		<td><font style="font-weight: bold;"><%=quocTich %></font></td>
	</tr>
	
	<tr>
		<td><liferay-ui:message key="ho-va-ten-cha" /></td>
		<td><font style="font-weight: bold;"><%=hoVaTenCha %></font></td>
		<td><liferay-ui:message key="ho-va-ten-me" /></td>
		<td><font style="font-weight: bold;"><%=hoVaTenMe %></font></td>
	</tr>
	<tr>
	<td>
	</td>
	<td colspan="3">
	
		<div align="left">
			<input type="button" class="btn btn-primary" onclick="javascript:submitForm(document.hrefFm,'<%=themTienSuURL.toString()%>');" value="<liferay-ui:message key="them-moi-tien-su" />" />
			<input type="button" class="btn" onclick="javascript:submitForm(document.hrefFm,'<%=portletURL.toString() %>');" value="<liferay-ui:message key="tro-ve" />" />
			
			<%
					TEMP_DoiTuongTienSu doiTuongTienSuTemp = TEMP_DoiTuongTienSuLocalServiceUtil.findTempDoiTuongTienSuMoiNhatByUserId(user.getUserId());
					if (Validator.isNotNull(doiTuongTienSuTemp)){
						TEMP_TienSu tienSuTemp = TEMP_TienSuLocalServiceUtil.findTienSuMoitNhatOfDoiTuongTiensuIdTemp(doiTuongTienSuTemp.getDoiTuongTienSuId());
						if (Validator.isNotNull(tienSuTemp)){
							if (tienSuTemp.getThaoTac().equals(Constants.DELETE)){
								
								%>
								<portlet:renderURL var="undoXoaTienSuRenderURL">
									<portlet:param name="mvcPath" value="/html/view.jsp"/>
									<portlet:param name="chonChucNangItem" value="<%=TienSuConstants.QUAN_LY_NGUOI_CO_TIEN_SU %>"/>
									<portlet:param name="tabs1" value="cap_nhat_tien_su"/>
									<portlet:param name="soDinhDanhCaNhan" value="<%=doiTuongTienSuTemp.getSoDinhDanhCaNhan() %>" />
									<portlet:param name="doiTuongTienSuId" value="<%=String.valueOf(doiTuongTienSuTemp.getDoiTuongTienSuId()) %>"/>
									<portlet:param name="tienSuId" value="<%=String.valueOf(tienSuTemp.getTienSuId()) %>"/>
									<portlet:param name="thaoTac" value="<%=tienSuTemp.getThaoTac() %>"/>									
									<portlet:param name="redirect" value="<%=redirect.toString() %>"/>
								</portlet:renderURL>
								<input type="button" class="btn btn-primary" onclick="javascript:submitForm(document.hrefFm,'<%=undoXoaTienSuRenderURL.toString()%>');" value="<liferay-ui:message key="un-do-xoa-tien-su" />" />
								
							<%} else {
								String key = "re-do-sua-tien-su";
								String undoRedo = ThaoTacUndoRedoEnum.REDO.getKey();
								if (Validator.isNull(tienSuTemp.getUndoRedo()) ||  tienSuTemp.getUndoRedo().equals(ThaoTacUndoRedoEnum.UNDO.getKey())){
									undoRedo = ThaoTacUndoRedoEnum.UNDO.getKey();
									key = "un-do-sua-tien-su";
								}
								%>
							 	<portlet:renderURL var="undoSuaTienSuRenderURL">
									<portlet:param name="mvcPath" value="/html/view.jsp"/>
									<portlet:param name="chonChucNangItem" value="<%=TienSuConstants.QUAN_LY_NGUOI_CO_TIEN_SU %>"/>
									<portlet:param name="tabs1" value="cap_nhat_tien_su"/>
									<portlet:param name="soDinhDanhCaNhan" value="<%=doiTuongTienSuTemp.getSoDinhDanhCaNhan() %>" />
									<portlet:param name="doiTuongTienSuId" value="<%=String.valueOf(doiTuongTienSuTemp.getDoiTuongTienSuId()) %>"/>
									<portlet:param name="tienSuId" value="<%=String.valueOf(tienSuTemp.getTienSuId()) %>"/>
									<portlet:param name="undoRedo" value="<%=undoRedo %>"/>		
									<portlet:param name="thaoTac" value="<%=tienSuTemp.getThaoTac() %>"/>									
									<portlet:param name="redirect" value="<%=redirect.toString() %>"/>
								</portlet:renderURL>
								<input type="button" class="btn btn-primary" onclick="javascript:submitForm(document.hrefFm,'<%=undoSuaTienSuRenderURL.toString()%>');" value="<liferay-ui:message key="<%=key %>" />" />
								 
							<%
							}
						}
					}
					%>
		</div>
	</td>
	</tr>
</table>


<liferay-ui:search-container emptyResultsMessage="khong-tim-thay-tien-su">
	<liferay-ui:search-container-results>
		<%
			results = TienSuLocalServiceUtil.findByDoiTuongTienSuId(doiTuongTienSuId);

			total = TienSuLocalServiceUtil.countByDoiTuongTienSuId(doiTuongTienSuId);

			pageContext.setAttribute("results", results);
			pageContext.setAttribute("total", total);
		%>
	</liferay-ui:search-container-results>
	
	<%
		int start = searchContainer.getDelta() * (searchContainer.getCur() - 1);
	%>
	
	<liferay-ui:search-container-row className="qldc.pm.tiensu.model.TienSu" keyProperty="tienSuId" modelVar="tienSu">
		<liferay-ui:search-container-column-text cssClass="aui-w3 txt-center" name="stt" value="<%=String.valueOf(++start)%>" />
		<liferay-ui:search-container-column-text cssClass="aui-w30" name="toi-danh" value="<%=tienSu.getToiDanh()%>" />
		<liferay-ui:search-container-column-text name="noi-dung-xu-phat" value="<%=tienSu.getHinhPhatChinh()%>" />
		<liferay-ui:search-container-column-text cssClass="aui-w10" name="ngay-toa-an" value="<%=DateUtils.printDate(tienSu.getNgayToaAn())%>" />
		<%
			String trangThai = TienSuLocalServiceUtil.kiemTraTrangThaiCuaTienSu(tienSu.getTienSuId()).getKey();
		%>
		
		<liferay-ui:search-container-column-text cssClass="aui-w15" name="trang-thai" ><liferay-ui:message key="<%=trangThai %>" /></liferay-ui:search-container-column-text>
		
		<liferay-ui:search-container-column-jsp cssClass="aui-w10 aui-Action" path="/html/tiensu/nguoi_co_tien_su/tien_su_actions.jsp" align="right" />
	</liferay-ui:search-container-row>

	<liferay-ui:search-iterator/>
</liferay-ui:search-container>

